package com.mifan.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mifan.common.Results;
import com.mifan.entity.Role;
import com.mifan.mapper.RoleMapper;
import com.mifan.service.IRoleService;
import com.mifan.vo.ResultData;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author Milan
 * @since 2024-09-13
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleMapper roleMapper;


    /**
     *  TODO 查询角色列表分页
     * @return
     */
    @GetMapping("/list")
    public Results list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.ne("id", 1);
        List<Role> roles = roleMapper.selectList(wrapper);
        PageInfo page = new PageInfo(roles);
        ResultData resultData = new ResultData();
        resultData.setPageNum((long) page.getPageNum());
        resultData.setPageSize((long) page.getPageSize());
        resultData.setList(page.getList());
        resultData.setTotal(page.getTotal());
        return Results.success(resultData);
    }


    /**
     * TODO 更新角色
     * @param role
     * @return
     */
    @PostMapping("update")
    public Results update(@RequestBody Role role) {
        int updateById = roleMapper.updateById(role);
        return Results.success(null);
    }

    /**
     * TODO 新增角色
     * @param role
     * @return
     */
    @PostMapping("save")
    public Results save(@RequestBody Role role) {
        int insert = roleMapper.insert(role);
        return Results.success(null);
    }

    /**
     * TODO 删除角色
     * @param id
     * @return
     */
    @PostMapping("delete")
    public Results delete(@RequestParam Long id) {
        int delete = roleMapper.deleteById(id);
        return Results.success(null);
    }

    /**
     * TODO 查询所有角色 不分页
     * @return
     */
    @GetMapping("all")
    public Results all() {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.ne("id", 1);
        List<Role> roles = roleMapper.selectList(wrapper);
        return Results.success(roles);
    }
}
